 <template lang="html">
  <div class="">
    <div class="search">
        <div class="input">
            <p>
                <label>筛选：</label>
                    优惠券名称：<el-input placeholder="请输入优惠券名称" v-model="searchData.contactName" ></el-input>&nbsp;&nbsp;
            </p>
            <p><el-button type="primary" class="se" style="margin-left:10px;" @click="search">搜索</el-button></p>
        </div>
    </div>
    <div class="add">
      <el-button type="primary" @click="add">创建优惠券</el-button>
      <el-button type="danger" @click="del">批量删除</el-button>
    </div>
    <tableVue :list="list" :state="true" v-loading="pageData.loading" :total="pageData.total"  @changeData="selectionVal" @sizeChange="handSize">
        <slot>
          <el-table-column label="操作" align="center">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  @click="compile(scope.row)">详情
                </el-button>
                 <el-button
                  type="primary"
                  size="mini"
                  @click="editor(scope.row)">编辑
                </el-button>
                 <el-button
                  type="danger"
                  size="mini"
                  @click="del(scope.row)">删除
                </el-button>
              </template>
          </el-table-column>
        </slot>
    </tableVue>
  </div>
</template>

<script>
  import Data from './discountData.js'
  import dialogVue from '@/components/dialog.vue'
  import tableVue from '@/components/alltable.vue'
  import {
   getDiscountList,
   delDiscount
  } from '@/api/discount/discount'
  export default {
    components : {
      tableVue,
      dialogVue
    },
    data () {
      return JSON.parse(JSON.stringify(Data))
    },
    mounted(){
        this.getAddList({current:'1',size:"10"});//获取地址列表 or 筛选
    },
    methods: {
        getAddList(data){ //获取地址列表
           getDiscountList(data,'get').then(res=>{
              if (res.data.code == 'OK') {
					this.pageData.loading =false;
					this.pageData.total = res.data.data.total;
					this.list.tbody = res.data.data.records.map(item=>{
                        item.gmtCreate = this.switchTime(item.gmtCreate)
                        item.gmtUpdate = this.switchTime(item.gmtUpdate);
                        item.receiveChannelDist=item.receiveChannelDist=="1"?"所有渠道都可以领取":"指定渠道可领取";
                        item.couponShape = item.couponShape == "10"?"现金":"折扣";
                        item.couponTpl = item.couponTpl =="1"?"默认模板":"";
                        item.receiveType = item.receiveType =="AUTO"?"系统下发":"用户主动领取";
                        item.useChannelDist=item.useChannelDist=="1"?"所有渠道都可以使用":"指定渠道可用";
                        item.logicalDeletion=item.logicalDeletion=="E"?"可用":'不可用';
                        switch(item.unProductType){
                                case "PTC1001":
                                    item.unProductType = "所有寄卖商品";
                                    break;
                                case "PTC1002":
                                        item.unProductType = "指定寄卖商品";
                                        break;
                                case "PTC1003":
                                        item.unProductType = "指定某种品牌商品";
                                        break;
                                case "PTC1004":
                                        item.unProductType = "指定某品牌商品";
                                        break;
                                case "PTG1001":
                                        item.unProductType = "全部出售商品";
                                        break;
                                case "PTG1002":
                                        item.unProductType = "指定出售商品";
                                        break;
                                case "PTG1003":
                                        item.unProductType = "指定某种出售品牌类商品";
                                        break;
                                case "PTG1004":
                                        item.unProductType = "指定某出售品牌商品";
                                        break;
                                default:
                                    break;
                        }
                        switch(item.productType){
                                case "PTC1001":
                                    item.unProductType = "所有寄卖商品";
                                    break;
                                case "PTC1002":
                                        item.unProductType = "指定寄卖商品";
                                        break;
                                case "PTC1003":
                                        item.unProductType = "指定某种品牌商品";
                                        break;
                                case "PTC1004":
                                        item.unProductType = "指定某品牌商品";
                                        break;
                                case "PTG1001":
                                        item.unProductType = "全部出售商品";
                                        break;
                                case "PTG1002":
                                        item.unProductType = "指定出售商品";
                                        break;
                                case "PTG1003":
                                        item.unProductType = "指定某种出售品牌类商品";
                                        break;
                                case "PTG1004":
                                        item.unProductType = "指定某出售品牌商品";
                                        break;
                                default:
                                    break;
                        }
                        switch(item.couponExpiredType){
                                case "CET1010":
                                    item.couponExpiredType = "固定日期";
                                    break;
                                case "CET1020":
                                        item.couponExpiredType = "领到券当日开始N天内有效";
                                        break;
                                case "CET1030":
                                        item.couponExpiredType = "领到券次日开始N天内有效";
                                        break;
                                default:
                                    break;
                        }
                        return item;
					});
				}
           })
        },
        compile(data){ //查看详情

            this.pageData.btnState ="2";
            this.$router.push({path:'./discountDetail',query:data})
        },
        editor(data){ //编辑单条数据

          this.$router.push({path:'./newDiscount',query:{id:data['couponId']}})
        },
        regionDetails(data){
            console.log(data)
        },
        closeWin(){
            this.pageData.dialog = false;
        },
        add(){
            this.$router.push({path:'/newDiscount'})
        },
        upForm(method){//编辑单条列表数据 or 新增
            this.$refs.takeData.validate((valid) => {
                if (valid) {
                    getRegionDetail(this.pageData.listData,method).then(res=>{
                        if(res.data.code=="OK"){
                            this.$message.success('修改成功！')
                            this.pageData.dialog = false;

                            this.getAddList();

                        }else{
                            this.$message.error(res.data.message)
                        }
                    })
                }
            })
        },
        search(){
            this.getAddList(this.searchData)
        },
        handSize(val){
            let data={
                current:val,
                size:"10"
            }
            this.getAddList(data)
        },
        del(data){
            if(!data.couponId){
                if(this.pageData.delId.length==0){
                    this.$message.error('请选择要删除的优惠券！')
                }else{
                    this.delMessage(()=>{
                        let arr=[]
                        this.pageData.delId.map(item=>{
                            arr.push(item.couponId)
                        })
                        arr.join(',')
                        delDiscount(arr).then(res=>{
                            if(res.data.code=="OK"){
                                this.getAddList({current:'1',size:"10"});
                            }else{
                                this.$message.error(res.data.message)
                            }
                        })
                    })
                }

            }else{
                 this.delMessage(()=>{
                    delDiscount(data.couponId).then(res=>{
                            if(res.data.code=="OK"){
                                this.getAddList({current:'1',size:"10"});
                            }else{
                                this.$message.error(res.data.message)
                            }
                    })
                 })

            }
        },
        selectionVal(val){
            if(val){
                this.pageData.delId = val
            }

        },


    }
  }
</script>

<style scoped>
  @import "../../../styles/common.css";
</style>
